package test07;

import java.util.*;

/**
 * 敏感词检测处理器
 */
public class SensitiveWordHandler extends ContentHandler {
    private Set<String> forbiddenWords;
    private Set<String> politicalTerms;
    private Set<String> violenceTerms;

    public SensitiveWordHandler() {
        super("敏感词检测器");
        initializeWordLists();
    }

    private void initializeWordLists() {
        forbiddenWords = new HashSet<>(Arrays.asList(
                "违禁词1", "违禁词2", "敏感信息", "非法内容"
        ));
        politicalTerms = new HashSet<>(Arrays.asList(
                "政治敏感词1", "政治敏感词2", "领导人姓名"
        ));
        violenceTerms = new HashSet<>(Arrays.asList(
                "暴力", "恐怖", "极端", "仇恨言论"
        ));
    }

    @Override
    protected boolean canHandle(ContentCheckRequest request) {
        // 主要处理文本内容
        return request.getContentType() == ContentType.TEXT ||
                request.getContentType() == ContentType.MULTIMODAL;
    }

    @Override
    protected CheckResult doCheck(ContentCheckRequest request) {
        String content = request.getContentData().toString().toLowerCase();

        // 检查违禁词
        for (String word : forbiddenWords) {
            if (content.contains(word.toLowerCase())) {
                return new CheckResult(false, RiskLevel.HIGH,
                        "发现违禁词: " + word, 0.95, "BLOCK");
            }
        }

        // 检查政治敏感词
        for (String term : politicalTerms) {
            if (content.contains(term.toLowerCase())) {
                return new CheckResult(false, RiskLevel.HIGH,
                        "发现政治敏感内容: " + term, 0.90, "BLOCK");
            }
        }

        // 检查暴力内容
        for (String term : violenceTerms) {
            if (content.contains(term.toLowerCase())) {
                return new CheckResult(false, RiskLevel.MEDIUM,
                        "发现暴力相关内容: " + term, 0.85, "REVIEW");
            }
        }

        return new CheckResult(true, RiskLevel.LOW, "未发现敏感词", 0.98, "PASS");
    }

    public void loadForbiddenWords(String filePath) {
        System.out.println("加载敏感词库: " + filePath);
        // 实际实现中从文件加载
    }
}

